package com.sihai.ehr.service.impl;

import java.text.MessageFormat;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Service;

import com.sihai.core.DataAccessor;
import com.sihai.core.DynamicQuery;
import com.sihai.core.Limit;
import com.sihai.core.LimitedList;
import com.sihai.core.QueryCreator;
import com.sihai.core.Sorter;
import com.sihai.ehr.model.biz.EmployeeinfoWork;
import com.sihai.ehr.model.biz.TrainPlanDutyTarget;
import com.sihai.ehr.service.TrainPlanDutyTargetService;

/**
 * 培训职务对象服务实现
 * 
 * @author henry
 * 
 */
@Service("trainPlanDutyTargetService")
public class TrainPlanDutyTargetServiceImpl implements
		TrainPlanDutyTargetService {
	@Resource
	DataAccessor dataAccessor;

	@Override
	public TrainPlanDutyTarget getTrainPlanDutyTarget(Long id) {
		if (id == null || id < 0) {
			throw new RuntimeException("【传入参数非法】");
		}
		try {
			return dataAccessor.get(TrainPlanDutyTarget.class, id);
		} catch (Exception e) {
			throw new RuntimeException(
					"【异常原因：获取培训职务对象明细数据库发生异常】【异常说明：+"
							+ e.getMessage()
							+ "】【发生地点：TrainPlanDutyTargetServiceImpl.getTrainPlanDutyTarget;参数id="
							+ id + " 】");
		}
	}

	@Override
	public LimitedList<TrainPlanDutyTarget> getTrainPlanDutyTargets(
			final Long planId, Limit limit, Sorter sorter) {
		// if (planId == null || planId < 0) {
		// throw new RuntimeException("【传入参数非法】");
		// }
		try {
			return dataAccessor.findByQuery(TrainPlanDutyTarget.class,
					new QueryCreator() {

						@Override
						public Query createQuery(Session session) {
							DynamicQuery dynamicQuery = new DynamicQuery(
									"from TrainPlanDutyTarget t where 1 <> 2");
							dynamicQuery.append("and t.trainPlan.id=" + planId);

							return dynamicQuery.build(session);
						}
					}, limit, sorter);
		} catch (Exception e) {
			throw new RuntimeException(
					"【异常原因：获取培训职务对象时数据库发生异常】【异常说明：+"
							+ e.getMessage()
							+ "】【发生地点：TrainPlanDutyTargetServiceImpl.getTrainPlanDutyTargets;参数planId="
							+ planId + "】");
		}
	}

	@Override
	public LimitedList<TrainPlanDutyTarget> getTrainPlanDutyTargetsByOrganizationId(
			final Long organizationId, Limit limit, Sorter sorter) {
		try {
			return dataAccessor.findByQuery(TrainPlanDutyTarget.class,
					new QueryCreator() {

						@Override
						public Query createQuery(Session session) {
							DynamicQuery dynamicQuery = new DynamicQuery(
									"from TrainPlanDutyTarget t where 1 <> 2");
							dynamicQuery.append("and t.organization.id="
									+ organizationId);

							return dynamicQuery.build(session);
						}
					}, limit, sorter);
		} catch (Exception e) {
			throw new RuntimeException(
					"【异常原因：获取培训职务对象时数据库发生异常】【异常说明：+"
							+ e.getMessage()
							+ "】【发生地点：TrainPlanDutyTargetServiceImpl.getTrainPlanDutyTargetsByOrgId;参数organizationId="
							+ organizationId + "】");
		}
	}

	@Override
	public void save(TrainPlanDutyTarget dutyTarget) {
		dutyTarget.getTrainPlan().getPlanStartDate().getYear();
		if (dutyTarget == null || dutyTarget.getTrainPlan() == null) {
			throw new RuntimeException("【dutyTarget对象参数异常】");
		}
		try {
			dataAccessor.saveOrUpdate(dutyTarget);
		} catch (Exception e) {
			throw new RuntimeException("【异常原因：保培训职务对象对象到数据库发生异常】【异常说明："
					+ e.getMessage()
					+ "】【发生地点：TrainPlanDutyTargetServiceImpl.save】");
		}
	}

	@Override
	public void deleteTrainPlanDutyTargetByPlanId(final Long planId) {
		if (planId == null || planId < 0) {
			throw new RuntimeException("【参数异常：planId为空】");
		}
		try {
			dataAccessor.batchUpdate(new QueryCreator() {
				@Override
				public Query createQuery(Session session) {
					return session.createQuery(MessageFormat
							.format("delete from TrainPlanDutyTarget t where t.trainPlan.id={0})",
									planId));
				}
			});
		} catch (Exception e) {
			throw new RuntimeException(
					"【异常原因：移出培训职务对象信息数据库发生异常】【异常说明："
							+ e.getMessage()
							+ "】【发生地点：TrainPlanDutyTargetServiceImpl.deleteTrainPlanDutyTargetByPlanId】");
		}
	}

	@Override
	public void deleteTrainPlanDutyTarget(final String ids) {
		if (ids == null || ids.isEmpty()) {
			throw new RuntimeException("【参数异常：ids为空】");
		}
		try {
			dataAccessor.batchUpdate(new QueryCreator() {
				@Override
				public Query createQuery(Session session) {
					return session.createQuery(MessageFormat
							.format("delete from TrainPlanDutyTarget t where t.id in ({0})",
									ids));
				}
			});
		} catch (Exception e) {
			throw new RuntimeException(
					"【异常原因：移出培训职务对象信息数据库发生异常】【异常说明："
							+ e.getMessage()
							+ "】【发生地点：TrainPlanDutyTargetServiceImpl.deleteTrainPlanDutyTarget】");
		}

	}

	@Override
	public void deleteTrainPlanDutyTargetByPlanIds(final String ids) {
		if (ids == null || ids.isEmpty()) {
			throw new RuntimeException("【参数异常：ids为空】");
		}
		try {
			dataAccessor.batchUpdate(new QueryCreator() {
				@Override
				public Query createQuery(Session session) {
					return session.createQuery(MessageFormat
							.format("delete from TrainPlanDutyTarget t where t.trainPlan.id in ({0})",
									ids));
				}
			});
		} catch (Exception e) {
			throw new RuntimeException(
					"【异常原因：移出培训职务对象信息数据库发生异常】【异常说明："
							+ e.getMessage()
							+ "】【发生地点：TrainPlanDutyTargetServiceImpl.deleteTrainPlanDutyTarget】");
		}
	}

	@Override
	public LimitedList<EmployeeinfoWork> getEmploymentsByPlanId(
			final Long planId, Limit limit, Sorter sorter) {
		try {
			return dataAccessor.findByQuery(EmployeeinfoWork.class,
					new QueryCreator() {

						@Override
						public Query createQuery(Session session) {
							DynamicQuery dynamicQuery = new DynamicQuery(
									"from EmployeeinfoWork e where e.duty.id in (select t.duty.id from TrainPlanDutyTarget t where 1 <> 2");
							dynamicQuery.append("and t.trainPlan.id=" + planId
									+ ")");

							return dynamicQuery.build(session);
						}
					}, limit, sorter);
		} catch (Exception e) {
			throw new RuntimeException(
					"【异常原因：获取培训职务对象员工时数据库发生异常】【异常说明：+"
							+ e.getMessage()
							+ "】【发生地点：TrainPlanDutyTargetServiceImpl.getEmploymentsByPlanId;参数planId="
							+ planId + "】");
		}
	}

	@Override
	public Double getTotalCountCreditByOrganizationId(
			final Long organizationId, final int year) {
		if (organizationId > 0) {
			try {
				return this.dataAccessor.findUnique(Double.class,
						new QueryCreator() {
							@Override
							public Query createQuery(Session session) {
								String hql = "select sum(t.credit) from TrainPlanDutyTarget t where t.organization.id="
										+ organizationId;
								if (year > 0) {
									hql += " and t.trainPlan.planStartDate between date_format('"
											+ year
											+ "-01-01 00:01','%Y-%c-%d %H:%i:%s')"
											+ " and date_format('"
											+ year
											+ "-12-31 23:59','%Y-%c-%d %H:%i:%s')";
								}
								return session.createQuery(hql);
							}
						});

			} catch (Exception e) {
				throw new RuntimeException(
						"【统计培训对象分数异常】【异常原因：:"
								+ e.getMessage()
								+ "】【发生地点：TrainPlanDutyTargetServiceImpl.getTotalCountCreditByOrganizationId】");
			}

		} else {
			return 0D;
		}
	}

}
